﻿@using ExcellentCRM.DataService.Models
@model ExcellentCRM.Site.Models.AccountModel
@{
    Layout = null;
}

@using (Ajax.BeginForm("CreateOrUpdate", null, new AjaxOptions
    {
        HttpMethod = "post",
        OnSuccess = "saveSucc"
    }, 
    new { @class = "form-horizontal", id = "acctSaveForm" }))
{
    @Html.AntiForgeryToken()
    <div id="alertContain" class="alert alert-error fade in hide">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        <div class="alert-content"></div>
    </div>
    <div class="control-group">
        @Html.LabelFor(m => m.AcctType, new { @class = "control-label" })
        <div class="controls">
            @{
                var acctTypeDic = Account.AccountTypeDic.Where(p => p.Key != AccountType.None);
                var acctTypeSelectList = new SelectList(acctTypeDic, "Key", "Value");
                var isEditing = Model != null && Model.Id.HasValue && Model.Id.Value > 0;
                var acctTypeAttrs = new Dictionary<string, object>
                    {
                        {"id", "slAcctType"},
                        {"class", "span3"},
                        {"data-bind", "value: acctType"}
                    };
                if (isEditing)
                {
                    acctTypeAttrs.Add("readonly", "readonly");
                }
            }
            @Html.DropDownListFor(m => m.AcctType, acctTypeSelectList, "选择", acctTypeAttrs)
            <span class="help-inline">
                @Html.ValidationMessageFor(m => m.AcctType)
            </span>
        </div>
    </div>
    <div class="control-group">
        @Html.LabelFor(m => m.AcctName, new { @class = "control-label" })
        <div class="controls">
            @Html.TextBoxFor(m => m.AcctName, null, new {placeholder = "帐号"})
            <span class="help-inline">
                @Html.ValidationMessageFor(m => m.AcctName)
            </span>
        </div>
    </div>
    <div class="control-group">
        @Html.LabelFor(m => m.AcctPass, new { @class = "control-label" })
        <div class="controls">
            @Html.PasswordFor(m => m.AcctPass, new {placeholder = "密码"})
            <span class="help-inline">
                @Html.ValidationMessageFor(m => m.AcctPass)
            </span>
        </div>
    </div>
    <div class="control-group">
        @Html.LabelFor(m => m.ConfirmPassword, new { @class = "control-label" })
        <div class="controls">
            @Html.PasswordFor(m => m.ConfirmPassword, new {placeholder = "确认密码"})
            <span class="help-inline">
                @Html.ValidationMessageFor(m => m.ConfirmPassword)
            </span>
        </div>
    </div>
    <fieldset id="staffContain" data-bind="fadeVisible: isStaff">
        <legend>普通员工信息</legend>
        <div class="control-group">
            <label class="control-label">
                所属部门
            </label>
            <div class="controls">
                @Html.DropDownListFor(m => m.Staff.DeptId,
                                      ViewBag.DeptList as IEnumerable<SelectListItem>, 
                                      "选择", new { @class = "span3" })
                <span class="help-inline">
                    @Html.ValidationMessageFor(m => m.Staff.DeptId)
                </span>
            </div>
        </div>
        <div class="control-group">
            @Html.LabelFor(m => m.Staff.StaffName, new { @class = "control-label" })
            <div class="controls">
                @Html.TextBoxFor(m => m.Staff.StaffName)
                <span class="help-inline">
                    @Html.ValidationMessageFor(m => m.Staff.StaffName)
                </span>
            </div>
        </div>
    </fieldset>
    <fieldset id="stuClientContain" data-bind="fadeVisible: isStuClient">
        <legend>学员信息</legend>
        <div class="control-group">
            @Html.LabelFor(m => m.StuClient.StudentInfo.StuName, new { @class = "control-label" })
            <div class="controls">
                @Html.TextBoxFor(m => m.StuClient.StudentInfo.StuName)
                <span class="help-inline">
                    @Html.ValidationMessageFor(m => m.StuClient.StudentInfo.StuName)
                </span>
            </div>
        </div>
        <div class="control-group">
            @Html.LabelFor(m => m.StuClient.StudentInfo.CellPhone, new { @class = "control-label" })
            <div class="controls">
                @Html.TextBoxFor(m => m.StuClient.StudentInfo.CellPhone)
                <span class="help-inline">
                    @Html.ValidationMessageFor(m => m.StuClient.StudentInfo.CellPhone)
                </span>
            </div>
        </div>
        <div class="control-group">
            @Html.LabelFor(m => m.StuClient.StudentInfo.Email, new { @class = "control-label" })
            <div class="controls">
                @Html.TextBoxFor(m => m.StuClient.StudentInfo.Email)
                <span class="help-inline">
                    @Html.ValidationMessageFor(m => m.StuClient.StudentInfo.Email)
                </span>
            </div>
        </div>
    </fieldset>
    <div class="control-group hidden">
        <div class="controls">
            <input type="submit" value="保存" class="btn btn-primary"/>
            @Html.HiddenFor(m => m.Id)
        </div>
    </div>
}
<script type="text/javascript">
    $.validator.setDefaults({
        ignore: ':not(.dk_container:visible + select):hidden'
    });
    
    $(function () {
        $('#acctSaveForm select').dropkick();
        $.validator.unobtrusive.parse('#acctSaveForm');
        ko.applyBindings(new SaveAcctModel(), $('#acctSaveForm')[0]);
    });

    function SaveAcctModel() {
        var self = this;
        self.acctType = ko.observable();
        self.isStaff = ko.computed(function () {
            return self.acctType() == 'Staff';
        });
        self.isStuClient = ko.computed(function () {
            return self.acctType() == 'Student';
        });
        self.isAdmin = ko.computed(function() {
            return self.acctType() == 'Admin';
        });
    }

    function saveSucc(result) {
        if (!result || (!result.success && !result.message)) {
            alert('保存失败，未知原因');
            return;
        }
        if (!result.success && result.message) {
            $('#alertContain .alert-content').html(result.message);
            $('#alertContain').removeClass('alert-success hide').addClass('alert-error').alert();
            return;
        }
        if (result.success) {
            $('#alertContain .alert-content').html('保存完成');
            $('#alertContain').removeClass('alert-error hide').addClass('alert-success').alert();
            $('#acctSaveForm #Id').val(result.expandos.id);
        }
    }
</script>